#! /bin/sh


#####################################
# READ PARAMS
#
app=$1
arch=$2


#####################################
# PREPARE variables:
#
# NOTE that "app"/Contents specify slash.
# this is not a problem even if app itself have slash at end.
# We will get something as  gg.app//Contents and this still works fine.
#
if [ "$arch" = "64" ] ; then
	app_vcomp="$app"/Contents/vcomponents_x64
	sys_vcomp=/usr/local/lib/vcomponents_x64
    dllsuff="_x64"
else
	app_vcomp="$app"/Contents/vcomponents
	sys_vcomp=/usr/local/lib/vcomponents
    dllsuff=""
fi

# YOU can change this to 0 to skip VREPORT.dll
install_vreports=1


#####################################
# COPY files of vcomponents into APP.
#####################################

if [ ! -d "$app_vcomp" ] ; then
	mkdir "$app_vcomp"
fi

# VSHARED
if [ ! -e "$app_vcomp"/libvshared_fat_release$dllsuff.dylib ] ; then
	cp $sys_vcomp/libvshared_fat_release$dllsuff.dylib 	"$app_vcomp"
fi

# VKERNEL
if [ ! -e "$app_vcomp"/libvkernel_fat_release$dllsuff.dylib ] ; then
	cp $sys_vcomp/libvkernel_fat_release$dllsuff.dylib 	"$app_vcomp"
fi

# VCLIENT
if [ ! -e "$app_vcomp"/libvclient_fat_release$dllsuff.dylib ] ; then
	cp $sys_vcomp/libvclient_fat_release$dllsuff.dylib 	"$app_vcomp"
fi

# VREPORT
if [ ! -e "$app_vcomp"/libvreport_fat_release$dllsuff.dylib ] ; then
	cp $sys_vcomp/libvreport_fat_release$dllsuff.dylib 	"$app_vcomp"
fi



####################################
# Copy Folders:
####################################

# vresources
cp -R $sys_vcomp/vresources 					"$app_vcomp"


#####################################
# CHANGE executable_path for engine dylibs:
#####################################

	# change id of VSHARED
	install_name_tool -id "@executable_path/../vcomponents$dllsuff/libvshared_fat_release$dllsuff.dylib" "$app_vcomp"/libvshared_fat_release$dllsuff.dylib

	# change id of VKERNEL
	install_name_tool -id "@executable_path/../vcomponents$dllsuff/libvkernel_fat_release$dllsuff.dylib" "$app_vcomp"/libvkernel_fat_release$dllsuff.dylib

	install_name_tool -change $sys_vcomp/libvshared_fat_release$dllsuff.dylib   "@executable_path/../vcomponents$dllsuff/libvshared_fat_release$dllsuff.dylib" 	"$app_vcomp"/libvkernel_fat_release$dllsuff.dylib
	install_name_tool -change $sys_vcomp/VSDK.framework/Versions/A/VSDK "@executable_path/../vcomponents/VSDK.framework/Versions/A/VSDK" 	"$app_vcomp"/libvkernel_fat_release$dllsuff.dylib

	# change id of VCLIENT
	install_name_tool -id "@executable_path/../vcomponents$dllsuff/libvclient_fat_release$dllsuff.dylib" "$app_vcomp"/libvclient_fat_release$dllsuff.dylib
	install_name_tool -change $sys_vcomp/libvshared_fat_release$dllsuff.dylib "@executable_path/../vcomponents$dllsuff/libvshared_fat_release$dllsuff.dylib" "$app_vcomp"/libvclient_fat_release$dllsuff.dylib

	# change id of VREPORT
	install_name_tool -id "@executable_path/../vcomponents$dllsuff/libvreport_fat_release$dllsuff.dylib" "$app_vcomp"/libvreport_fat_release$dllsuff.dylib
	install_name_tool -change $sys_vcomp/libvshared_fat_release$dllsuff.dylib  "@executable_path/../vcomponents$dllsuff/libvshared_fat_release$dllsuff.dylib" 	"$app_vcomp"/libvreport_fat_release$dllsuff.dylib
	install_name_tool -change $sys_vcomp/libvkernel_fat_release$dllsuff.dylib  "@executable_path/../vcomponents$dllsuff/libvkernel_fat_release$dllsuff.dylib" 	"$app_vcomp"/libvreport_fat_release$dllsuff.dylib
	install_name_tool -change $sys_vcomp/libvclient_fat_release$dllsuff.dylib "@executable_path/../vcomponents$dllsuff/libvclient_fat_release$dllsuff.dylib" 	"$app_vcomp"/libvreport_fat_release$dllsuff.dylib


#####################################
# now we need update yet App/Contents/Frameworks/V4RB.rbx_0_xxx.dylib
# Xojo(REALBasic) can assign any digits xxx, so we use globbing (regex) here to locate file.
# NOTE: we do cd into folder Frameworks to work with short files names and avoid absolute paths,
# which can have spaces, and this cause problem with install_name_tool.
#####################################
app_frmws="$app"/Contents/Frameworks
cd "$app_frmws"

for file in ./V4RB*
do
	v4rb_dylib="$file"
	break
done

if [ ! -e $v4rb_dylib ] ; then		# we try low case: at some point Xojo started to put v4rb_cocoa.dylib
	for file in ./v4rb*
	do
		v4rb_dylib="$file"
		break
	done
fi


#
# now we can change paths In the V4RB.dylib
#
install_name_tool -change $sys_vcomp/libvshared_fat_release$dllsuff.dylib 	"@executable_path/../vcomponents$dllsuff/libvshared_fat_release$dllsuff.dylib" 		"$v4rb_dylib"
install_name_tool -change $sys_vcomp/libvkernel_fat_release$dllsuff.dylib 	"@executable_path/../vcomponents$dllsuff/libvkernel_fat_release$dllsuff.dylib" 		"$v4rb_dylib"
install_name_tool -change $sys_vcomp/libvclient_fat_release$dllsuff.dylib 	"@executable_path/../vcomponents$dllsuff/libvclient_fat_release$dllsuff.dylib" 		"$v4rb_dylib"
install_name_tool -change $sys_vcomp/libvreport_fat_release$dllsuff.dylib 	"@executable_path/../vcomponents$dllsuff/libvreport_fat_release$dllsuff.dylib" 		"$v4rb_dylib"
